<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    //第一种方法
    // let str = 'password=123456&username=xiaoming&a23=456&a24=457';
    // let newArr = str.split('&');
    // let arr = newArr.toString().split('=');
    // let arr1 = arr.toString().split(',');
    // console.log(arr1);

    // var obj = {};
    // for(let i = 0;i < arr1.length/2;i++){
    //     obj[arr1[2*i]] = arr1[2*i+1];
    // }


    // let arr2 = [];
    // for(let attr in obj){
    //     arr2.push(attr+'='+obj[attr]);
    // }

    // let str1 = arr2.join('&');
    // console.log(str1);


    //鹏哥方法
    let str = 'password=123456&username=xiaoming&a23=456&a24=457';
    let newArr = str.split('&');
    let ret = {};
    for(let i = 0;i < newArr.length;i++){
        let child = newArr[i].split('=');
        
        if(Number(child[1])){
            ret[child[0]] = Number(child[1]);
        }else{
            ret[child[0]] = child[1]
        }
    }

    console.log(ret);

    let str1 = ''
    for(let attr in ret){

        if(str1){
            str1 += "&" + attr + "="+ ret[attr];
        }else{
            str1 += attr + "="+ ret[attr];
        }

    }

    console.log(str1);
</script>

</html>